package me.jessyan.armscomponent.app.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import me.jessyan.armscomponent.commonsdk.utils.Utils;
import me.jessyan.armscomponent.app.R;
import me.jessyan.armscomponent.app.di.component.DaggerLearningHomeComponent;
import me.jessyan.armscomponent.app.mvp.contract.learn.LearningHomeContract;
import me.jessyan.armscomponent.app.mvp.presenter.learn.LearningHomePresenter;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * 学习主页 - 101
 * Author: Taozebi
 * Date: 08/16/2021 18:12
 * Description: describe of this class
 */

@Route(path = RouterHub.LEARNING_HOMEFRAGMENT)
public class LearningHomeFragment extends BaseFragment<LearningHomePresenter> implements LearningHomeContract.View {

    @BindView(R.id.learning_recent_ly)
    LinearLayout recentLy;

    @BindView(R.id.learning_course_ly)
    LinearLayout courseLy;

    @BindView(R.id.learning_plan_ly)
    LinearLayout planLy;

    public static LearningHomeFragment newInstance() {
        LearningHomeFragment fragment = new LearningHomeFragment();
        return fragment;
    }

    @OnClick({R.id.learning_add_sign,R.id.learning_message,
            R.id.learning_recently,R.id.learning_my_course,R.id.learning_plan,R.id.home_title_suggest_ly})
    void viewClick(View view) {
        int id = view.getId();
        if(id == R.id.learning_add_sign){
            if(Utils.isFastClick()){
                ARouter.getInstance().build(RouterHub.LEARNING_SIGNACTIVITY).navigation();
            }
        }else if(id == R.id.learning_message){
            if(Utils.isFastClick()){
                ARouter.getInstance().build(RouterHub.MINE_MSGCENTERACTIVITY).navigation();
            }
        }else if(id == R.id.learning_recently){
            if(Utils.isFastClick()){
                ARouter.getInstance().build(RouterHub.LEARNING_MYRECITEACTIVITY).navigation();
            }
        }else if(id == R.id.learning_my_course){
            if(Utils.isFastClick()){
                Toast.makeText(mContext, "我的课程", Toast.LENGTH_SHORT).show();
            }
        }else if(id == R.id.learning_plan){
            if(Utils.isFastClick()){
                ARouter.getInstance().build(RouterHub.LEARNING_MYPLANACTIVITY).navigation();
            }
        }else if (id == R.id.home_title_suggest_ly) {
            if(Utils.isFastClick()){
                ARouter.getInstance().build(RouterHub.LEARNING_SUGGESTLEARNACTIVITY).navigation();
            }
        }
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerLearningHomeComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.learning_fragment_home, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        fillRecentLy();//最近在学
        fillCourse();//我的课程
        fillPlan();//学习计划
    }

    private void fillRecentLy(){
        recentLy.removeAllViews();
        for (int i = 0; i < 2; i++) {
            LinearLayout recentLyItem = (LinearLayout) getLayoutInflater().inflate(R.layout.layout_list_item1, null);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.setMargins(0, 10, 10, 10);
            recentLyItem.setLayoutParams(lp);
            recentLy.addView(recentLyItem);
        }
    }

    private void fillCourse(){
        courseLy.removeAllViews();
        for (int i = 0; i < 4; i++) {
            LinearLayout recentLyItem = (LinearLayout) getLayoutInflater().inflate(R.layout.layout_list_item2, null);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.setMargins(8, 5, 8, 0);
            recentLyItem.setLayoutParams(lp);
            //recentLyItem.setWeightSum(1);
            courseLy.addView(recentLyItem);
        }
    }

    private void fillPlan(){
        planLy.removeAllViews();
        for (int i = 0; i < 4; i++) {
            LinearLayout recentLyItem = (LinearLayout) getLayoutInflater().inflate(R.layout.layout_list_item2, null);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.setMargins(8, 5, 8, 0);
            recentLyItem.setLayoutParams(lp);
            planLy.addView(recentLyItem);
        }
    }

    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }
}
